package org.geektimes.projects.user.web.servlet;

import org.geektimes.projects.user.management.Hello;

import javax.management.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import java.lang.management.ManagementFactory;

public class JMXServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        super.init();
        // 获取平台 MBean Server
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        // 为 UserMXBean 定义 ObjectName
        try {
            ObjectName objectName = new ObjectName("jolokia:type=Hello");
            mBeanServer.registerMBean(new Hello("Hello World"), objectName);
        } catch (MalformedObjectNameException e) {
            e.printStackTrace();
        } catch (NotCompliantMBeanException e) {
            e.printStackTrace();
        } catch (InstanceAlreadyExistsException e) {
            e.printStackTrace();
        } catch (MBeanRegistrationException e) {
            e.printStackTrace();
        }
    }
}
